package org.footballmanager.pandafootball.system.controller;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootYys;
import org.footballmanager.pandafootball.system.form.impl.FootYysSearchForm;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.footballmanager.pandafootball.system.service.FootYysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@SessionAttributes("footYysSearchForm")
@RequestMapping("/foot/footYys")
public class FootYysController extends AddAndEditController<FootYys>{
	/* 实体名称 */
	private static final String ENTITYNAME = "footYys";
	private static final String EDITPAGE = String.format(Constants.EDITPAGE, Constants.FOOT, ENTITYNAME);
	private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.FOOT, ENTITYNAME);
	private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.FOOT, ENTITYNAME);
	private static final String REDIRECT = String.format(Constants.REDIRECT, Constants.FOOT, ENTITYNAME);

	@Autowired
	private FootYysService footYysService;
	
	@Autowired
	private FootUserService footUserService;

    /**
     * 抽象方法
     *
     * @return 实体名称
     */
    @Override
    protected String getEntityName() {
        return ENTITYNAME;
    }

    /**
     * 初始化
     * @param map 参数
     * @param footYysSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, HttpServletRequest request, @ModelAttribute FootYysSearchForm footYysSearchForm){
        map.addAttribute("userList", footUserService.getAllWithoutAdmin(request));
        FootYysSearchForm newFootYysSearchForm = new FootYysSearchForm();
        if(footYysSearchForm != null){
            newFootYysSearchForm = footYysSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootYysSearchForm);
    }

	@RequestMapping(value="/search")
	public String search(Model map, @ModelAttribute FootYysSearchForm footYysSearchForm) {
        footYysService.search(ENTITYNAME, map, footYysSearchForm);
		return LISTPAGE;
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String delete(@RequestParam Long id, HttpServletRequest request) {
		footYysService .delete(id, request);
		return REDIRECT;
	}

	@RequestMapping(value = "/deleteall", method = RequestMethod.POST)
	public String deleteall(HttpServletRequest request, @ModelAttribute FootYysSearchForm footYysSearchForm) {
        footYysService.deleteAll(footYysSearchForm, request);
		return REDIRECT;
	}
	
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String openEditPage(Model map, @RequestParam Long id) {
		map.addAttribute(ENTITYNAME, footYysService.get(id));
		return EDITPAGE;
	}
	
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootYysSearchForm footYysSearchForm) {
		map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footYysSearchForm);
		map.addAttribute(ENTITYNAME, footYysService.get(id));
		return VIEWPAGE;
	}	
		
}
